//
//  HMStatusCell.swift
//  sinaweibo
//
//  Created by WGP on 16/4/14.
//  Copyright © 2016年 WGP. All rights reserved.
//

import UIKit

import SnapKit

class HMStatusCell: UITableViewCell {
    
//    记录bottomView的top约束
    var bottomViewTopCons: Constraint?
  
    var status: HMStatus?
    {
        didSet
        {
//            当控制器给cell设置给模型后,来传给originalView
            originalView.status = status
            
//            先卸载掉之前bottomView的底部约束,修改约束的参照必须要先卸载
            bottomViewTopCons?.uninstall()
            
            if let retweetStatus = status?.retweeted_status
            {
                retweetView.hidden = false
                retweetView.retweetStatus = retweetStatus
                
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
            }else
            {
                retweetView.hidden = true
                
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    bottomViewTopCons = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupSubViews()
        
    }
    
    private func setupSubViews()
    {
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
        
//        1.添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        
//        2.添加约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(10)
            make.left.right.equalTo(contentView)
            
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
        }
        
        bottomView.snp_makeConstraints { (make) -> Void in
            make.right.left.equalTo(contentView)
            make.height.equalTo(37)
            
            bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.left.top.right.equalTo(self)
            make.bottom.equalTo(bottomView)
        }
    }
    
//    MARK: - 懒加载
    
    private lazy var originalView: HMStatusOriginalView = HMStatusOriginalView()
    
//    被转发微博
    private lazy var retweetView: HMStatusRetweetView = HMStatusRetweetView()
    
    //    底部视图
    private lazy var bottomView: HMStatusBottomView = HMStatusBottomView()

}
